import { storeToRefs } from 'pinia'
import { useHeadStore } from '@/store/head'

export default (to, from, failure) => {
  const { alternates } = storeToRefs(useHeadStore())
  const { query, path } = to

  const pageUrl = import.meta.env.VITE_APP_URL + path

  const nav = path.split('/').pop()
  alternates.value.forEach((item) => {
    if (item.hreflang === 'x-default') item.href = `${import.meta.env.VITE_APP_URL}/en/${nav}`
    else item.href = `${import.meta.env.VITE_APP_URL}/${item.hreflang}/${nav}`
  })
}

function filterQueryString(query) {
  const allowQueries = ['keyword', 'page', 'name', 'lang']
  if (!Object.keys(query).some((q) => allowQueries.includes(q))) return ''
  return (
    '?' +
    Object.entries(query)
      .filter((item) => allowQueries.includes(item[0]))
      .map(([key, value]) => `${key}=${value}`)
      .join('&')
  )
}
